﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerManager : MonoBehaviour
{
    private GameObject m_cube;
    private String currentCube;

    private String[] cubeEnum =
    {
        "cube",
        "cube_battery",
        "cube_books",
        "cube_box",
        "cube_bread",
        "cube_brick",
        "cube_cake",
        "cube_car",
        "cube_cheese",
        "cube_dice",
        "cube_fruit",
        "cube_fruit1",
        "cube_hamburger",
        "cube_icecube",
        "cube_jar",
        "cube_mushroom",
        "cube_piois",
        "cube_safe",
        "cube_sushi",
        "cube_toster",
        "cube_watermelon"
    };

    private void Awake()
    {
        currentCube = PlayerPrefs.GetString("Player", "cube");
    }

    void Start()
    {
        if (currentCube == "cube")
        {
            BuyCube(0);
        }
        m_cube = Instantiate(Resources.Load(currentCube) as GameObject, transform.position, transform.rotation);
        m_cube.transform.SetParent(gameObject.transform);
    }

    public void BuyCube(int cubeNum)
    {
        PlayerPrefs.SetInt(cubeEnum[cubeNum], 1);
    }

    public int GetCurrentCube()
    {
        for(int i = 0; i < cubeEnum.Length; i++)
        {
            if (cubeEnum[i] == currentCube)
            {
                return i;
            }
        }
        return 0;
    }

    public bool HaveCube(int cubeNum)
    {

        if (PlayerPrefs.GetInt(cubeEnum[cubeNum], 0) == 1)
        {
            return true;
        }
        return false;
    }

    public void ChangeCube(int cubeNum)
    {
        currentCube = cubeEnum[cubeNum];
        PlayerPrefs.SetString("Player", currentCube);
        m_cube = Instantiate(Resources.Load(currentCube) as GameObject, transform.position, transform.rotation);
        m_cube.transform.SetParent(gameObject.transform);
    }

    public void DestoryCube()
    {
        Destroy(m_cube.gameObject);
    }
}
